package com.gyms.activity;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.classic.okhttp.AppChangeUtils;
import com.classic.okhttp.enums.Enum;
import com.classic.okhttp.services.IUserService;
import com.classic.okhttp.zmoumall.http.ObjectCallback;
import com.classic.okhttp.zmoumall.utils.ToastUtil;
import com.gyms.R;
import com.gyms.base.MyAutoLayoutActivity;
import com.gyms.constant.Constants;
import com.gyms.manager.TitleManager;

import butterknife.BindView;
import butterknife.OnClick;
import utils.StringUtils;

/**
 * 举报页面
 * yjl
 */
public class ReportActivity extends MyAutoLayoutActivity {
    @BindView(R.id.et_report)
    EditText etReport;
    @BindView(R.id.tv_count)
    TextView tvCount;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    private String lable;
    private String venueId;

    private boolean isReport;//是否是举报页面

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_report;
    }

    @Override
    protected void initView() {
        createLoadingView("提交中", false);
        venueId = getIntent().getStringExtra(Constants.FLAG_INTENT_VENUEID);
        lable = getIntent().getStringExtra(Constants.LABLE);
        tvTips.setText(AppChangeUtils.REPORT_TEXT);
        if (lable != null) {
            setTitle(lable);
            etReport.setHint("请填写您的意见或建议");
            tvTips.setVisibility(View.GONE);
        } else {
            setTitle("举报");
            isReport=true;
            mTitleManager.setLeftCloseImg(R.drawable.close_selector);
        }
        mTitleManager.setRightText("提交").setBackgroundResource(R.drawable.sp_w_blue_commit);
        mTitleManager.getmRightTV().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = etReport.getText().toString();
                if (StringUtils.isNull(text)) {
                    if (lable != null) {
                        ToastUtil.showToast(mContext, "请输入您的意见或建议！");
                    } else {
                        ToastUtil.showToast(mContext, "请输入举报内容！");
                    }
                    return;
                } else {
                    if (lable != null) {
                        reqReport(text, Enum.EnumFeedBackType.Feedback);
                    } else {
                        reqReport(text, Enum.EnumFeedBackType.Report);
                    }
                }


            }
        });
        setBack();

    }

    @Override
    protected void setTitleTheme() {
        mTheme = TitleManager.TitleTheme.White;
    }

    @Override
    protected void setListener() {
        etReport.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                tvCount.setText(etReport.getText().toString().length() + "/140");
            }
        });
    }

    @Override
    protected void processLogic() {

    }


    @OnClick(R.id.et_report)
    public void onClick() {
        if (!etReport.isFocusable()) {
            showKeyboard();
            etReport.setFocusable(true);
            etReport.setFocusableInTouchMode(true);
            etReport.requestFocus();
            etReport.findFocus();
        }
    }

    private void reqReport(String content, Enum.EnumFeedBackType type) {
        loadingView.show();
        IUserService.addFeedBack(this, content, venueId, type, false, new ObjectCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean response, String message) {
                dialogDis();
                ToastUtil.showToast(mContext, message);
                onBackPressed();
            }

            @Override
            public void onErrorT(int code, Boolean response, String errorMessage) {
                dialogDis();
                ToastUtil.showToast(mContext, errorMessage);
            }

            @Override
            public void onHttpErrorCall(int statusCode) {
                dialogDis();
            }
        });
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        hideKeyBoard(ev);//隐藏软键盘
        return super.dispatchTouchEvent(ev);
    }
    /**
     * 隐藏软键盘
     */
    protected void showKeyboard() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if(isReport){
            overridePendingTransition(R.anim.activity_close_in, R.anim.activity_close_out);
        }
    }
}
